<?php
$page_title = '用户首页 - 考务管理系统';

// 开启会话
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// 检查用户是否已登录
if (!isset($_SESSION['user_id']) || !isset($_SESSION['username'])) {
    header('Location: index.php');
    exit();
}

// 获取用户基本信息
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'] ?? $user_id;

// 尝试连接数据库（安全方式）
$conn = null;
$db_connected = false;

try {
    require_once 'database.php';
    if (isset($conn) && $conn instanceof mysqli) {
        $db_connected = true;
    }
} catch (Exception $e) {
    error_log("数据库连接失败: " . $e->getMessage());
    $db_connected = false;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title; ?></title>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">

    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background-color: #f8f9fa;
        }

        .main-content {
            margin-top: 20px;
        }

        .card {
            border: none;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            margin-bottom: 24px;
        }

        .card-title {
            margin: 0 0 20px 0;
            font-size: 20px;
            font-weight: 600;
            color: #1f2937;
        }

        .welcome-section {
            text-align: center;
            padding: 40px 20px;
        }

        .welcome-title {
            font-size: 32px;
            color: #1f2937;
            margin-bottom: 10px;
        }

        .welcome-subtitle {
            font-size: 16px;
            color: #6b7280;
            margin-bottom: 30px;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            background: #f9fafb;
            border-radius: 6px;
            border-left: 4px solid #0d6efd;
        }

        .info-label {
            font-weight: 500;
            color: #374151;
        }

        .info-value {
            font-weight: 600;
            color: #1f2937;
        }

        .status-indicator {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
        }

        .status-online .status-dot {
            background-color: #198754;
        }

        .status-offline .status-dot {
            background-color: #dc3545;
        }

        .status-online {
            color: #198754;
        }

        .status-offline {
            color: #dc3545;
        }

        .actions-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
        }

        .action-btn {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 10px;
            padding: 20px;
            background: white;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            text-decoration: none;
            color: #495057;
            transition: all 0.3s;
        }

        .action-btn:hover {
            border-color: #0d6efd;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(13, 110, 253, 0.2);
            color: #0d6efd;
            text-decoration: none;
        }

        .action-icon {
            font-size: 32px;
        }

        .action-text {
            font-weight: 500;
            text-align: center;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <?php include_once 'common/navigation.php'; ?>

    <!-- 主要内容 -->
    <div class="container main-content">
        <!-- 欢迎区域 -->
        <div class="card">
            <div class="card-body">
                <div class="welcome-section">
                    <h1 class="welcome-title">欢迎回来，<?php echo htmlspecialchars($username); ?>！</h1>
                    <p class="welcome-subtitle">这里是您的个人工作台</p>
                </div>
            </div>
        </div>

        <!-- 系统状态 -->
        <div class="card">
            <div class="card-body">
                <h2 class="card-title">系统状态</h2>
                <?php if (!$db_connected): ?>
                    <div class="alert alert-warning" role="alert">
                        <i class="fas fa-exclamation-triangle"></i> 数据库连接异常，部分功能可能不可用。请联系系统管理员。
                    </div>
                <?php else: ?>
                    <div class="alert alert-success" role="alert">
                        <i class="fas fa-check-circle"></i> 系统运行正常，所有功能可用。
                    </div>
                <?php endif; ?>

                <div class="info-grid">
                    <div class="info-item">
                        <span class="info-label">用户ID</span>
                        <span class="info-value"><?php echo htmlspecialchars($user_id); ?></span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">用户名</span>
                        <span class="info-value"><?php echo htmlspecialchars($username); ?></span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">登录时间</span>
                        <span class="info-value"><?php echo date('Y-m-d H:i:s'); ?></span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">数据库状态</span>
                        <span class="status-indicator <?php echo $db_connected ? 'status-online' : 'status-offline'; ?>">
                            <span class="status-dot"></span>
                            <?php echo $db_connected ? '正常' : '异常'; ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <!-- 快捷操作 -->
        <div class="card">
            <div class="card-body">
                <h2 class="card-title">快捷操作</h2>
                <div class="actions-grid">
                    <a href="exam_signup_list.php" class="action-btn">
                        <div class="action-icon">📝</div>
                        <div class="action-text">监考报名</div>
                    </a>
                    <a href="exam_projects.php" class="action-btn">
                        <div class="action-icon">📊</div>
                        <div class="action-text">考试项目</div>
                    </a>
                    <a href="create_exam_project.php" class="action-btn">
                        <div class="action-icon">➕</div>
                        <div class="action-text">新建项目</div>
                    </a>
                    <a href="rooms_display.php" class="action-btn">
                        <div class="action-icon">🏛️</div>
                        <div class="action-text">课室管理</div>
                    </a>
                    <a href="serving_staff.php" class="action-btn">
                        <div class="action-icon">👥</div>
                        <div class="action-text">员工管理</div>
                    </a>
                    <a href="dashboard.php" class="action-btn">
                        <div class="action-icon">📈</div>
                        <div class="action-text">系统仪表板</div>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>

    <?php
    // 安全关闭数据库连接
    if ($db_connected && isset($conn)) {
        $conn->close();
    }
    ?>
</body>
</html>